//
//  SwtichButton.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/3/20.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "SwtichButton.h"


@interface  SwtichButton()

@property (nonatomic,copy)   NSString *buttonTitle;
@property (nonatomic,strong) UIImage  *normalImage;
@property (nonatomic,strong) UIImage  *highlightImage;


@end


@implementation SwtichButton



+ (instancetype)buttonWithNormalImage:(NSString *)normalName
                          highlighted:(NSString *)highlightName{
    SwtichButton *btn = [super buttonWithType:UIButtonTypeCustom];
    if (btn) {
        btn.normalImage = [UIImage imageNamed:normalName];
        btn.highlightImage = [UIImage imageNamed:highlightName];
        [btn initializeUI];
        btn.adjustsImageWhenDisabled = NO;
        
        [btn addTarget:btn action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}


- (void)initializeUI{
    [self setImage:self.normalImage forState:UIControlStateNormal];
    _isSelectedStatus = NO;
}

- (void)clickButton{
    _isSelectedStatus = !_isSelectedStatus;
    if (_isSelectedStatus ) {
        [self setImage:self.highlightImage forState:UIControlStateNormal];
    }else{
        [self setImage:self.normalImage forState:UIControlStateNormal];
    }
    if (self.clickEvent) {
        self.clickEvent(_isSelectedStatus);
    }
}



- (void)setIsSelectedStatus:(BOOL)isSelectedStatus{
    if(_isSelectedStatus != isSelectedStatus){
        _isSelectedStatus = isSelectedStatus;
        if (isSelectedStatus) {
            [self setImage:self.highlightImage forState:UIControlStateNormal];
        }else{
            [self setImage:self.normalImage forState:UIControlStateNormal];
        }
    }
}


@end
